//
//  DrawingView.m
//  TestIPhone2
//
//  Created by Davide Spano on 10/19/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "DrawingView.h"


@implementation DrawingView


- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
		[self setMultipleTouchEnabled:YES];
		[self setUserInteractionEnabled:YES];
        // Initialization code.
		CGFloat width = frame.size.width;
		CGFloat height = frame.size.height;
		colorSpace = CGColorSpaceCreateDeviceRGB();
		rawData = (unsigned char*) malloc(height * width * 4);
		memset(rawData, 0, height * width * 4);
		
		NSUInteger bytesPerPixel = 4;
		NSUInteger bytesPerRow = bytesPerPixel * width;
		NSUInteger bitsPerComponent = 8;
		context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
		CGContextClearRect(context, frame);
		CGContextSetRGBFillColor(context,1.0,1.0,1.0,1.0);
		
		CGContextFillRect(context, CGRectMake(0, 0, width, height));
		CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
		CGContextBeginPath(context);
		CGContextMoveToPoint(context, 0, 1.0);
		CGContextAddLineToPoint(context, width, 1.0);
		CGContextStrokePath(context);
		zoom = 1.0;
		translate = CGPointMake(0.0, 0.0);
		
    }
    return self;
}


- (void) clear{
	CGFloat width = self.bounds.size.width;
	CGFloat height = self.bounds.size.height;
	CGContextSetRGBFillColor(context,1.0,1.0,1.0,1.0);
	
	CGContextFillRect(context, CGRectMake(0, 0, width, height));
	CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
	CGContextBeginPath(context);
	CGContextMoveToPoint(context, 0, 1.0);
	CGContextAddLineToPoint(context, width, 1.0);
	CGContextStrokePath(context);
	[self setNeedsDisplay];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code.
	CGContextRef g = UIGraphicsGetCurrentContext();
	// clear screen
	CGContextSetRGBFillColor(g,0.9,0.9,0.9,1.0);
	CGContextClipToRect(g, rect);
	CGContextFillRect(g, rect);
	
	// draw the view
	CGContextTranslateCTM(g, translate.x, translate.y);
	CGContextScaleCTM(g, zoom, zoom);
	CGImageRef image = CGBitmapContextCreateImage(context);
	CGContextDrawImage(g, self.bounds, image);
	//[NSThread sleepForTimeInterval:0.10];
	CGImageRelease(image);
}

- (void)  drawLine:(float)fromX :(float)fromY :(float)toX :(float)toY{
	CGContextBeginPath(context);
	
	CGPoint from = [self locationInCanvas: CGPointMake(fromX, fromY)];
	CGPoint to = [self locationInCanvas: CGPointMake(toX, toY)];
	CGContextMoveToPoint(context, from.x, from.y);
	CGContextAddLineToPoint(context, to.x, to.y);
	CGContextStrokePath(context);
	CGPoint upLeft = CGPointMake(fromX < toX ? fromX - 5 : toX - 5,
								 fromY < toY ? fromY - 5 : toY - 5);
	CGPoint refSize = CGPointMake(abs(fromX - toX)+ 10 , abs(fromY - toY) + 10);
	[self setNeedsDisplayInRect:CGRectMake(upLeft.x, upLeft.y, refSize.x, refSize.y)];
}

- (CGPoint) locationInCanvas: (CGPoint) viewPoint{
	CGPoint toRet = CGPointMake(viewPoint.x, viewPoint.y);
	toRet.x = viewPoint.x - translate.x;
	toRet.y = viewPoint.y - translate.y;
	toRet.x = toRet.x / zoom;
	toRet.y = toRet.y / zoom;
	return toRet;
}

- (void) setZoom:(float)z{
	if(z != 0.0){
		zoom = z;
		[self setNeedsDisplay];
		
	}
}

- (float) getZoom{
	return zoom;
}


- (void) setTranslate:(CGPoint) t{
	translate.x = t.x;
	translate.y = t.y;
	[self setNeedsDisplay];
}

- (CGPoint) getTranslate{
	return translate;
}

- (void)dealloc {
	CGColorSpaceRelease(colorSpace);
	free(rawData);
	CGContextRelease(context);
	
}

@end
